//
//  RBGroupListVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/27.
//

#import "RBGroupListVC.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "RBPager.h"
#import <UIView+YYAdd.h>
#import "RBImageBannerView.h"
#import "RBGroupListCell.h"
#import "RBGroupDetailVC.h"
#import "UIImage+RBAdd.h"
#import "RBGroupFilterPopVC.h"
#import "RBAppDelegateHelper.h"
#import "RBGroupListResponse.h"
#import "RBRefreshHeader.h"
#import "RBRefreshFooter.h"

@interface RBGroupListVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong)UIView *filterView;
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableArray *dataSource;

@property(nonatomic, strong) RBGroupListResponse *response;

@end

@implementation RBGroupListVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self.listView.mj_header beginRefreshing];
}

#pragma mark - Private

- (void)setup
{
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
}

- (void)fillData
{
    [self.dataSource removeAllObjects];
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.sectionInset = UIEdgeInsetsMake(Zoom(10), Zoom(12), Zoom(10), Zoom(12));
        group.minimumLineSpacing = Zoom(10);
        group.cellModels = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
        group;
    })];
    [self appendData];
    
}

- (void)appendData
{
    __weak typeof(self) weak_self = self;
    NSMutableArray *arrayM = ({
        RBGroupModel *group = self.dataSource.firstObject;
        (NSMutableArray *)group.cellModels;
    });
    for (id model in self.response.data)
    {
        [arrayM addObject:({
            RBCellModel *cellModel = RBCellModel.new;
            cellModel.cellData = model;
            cellModel.reuseCellIdentifier = NSStringFromClass(RBGroupListCell.class);
            cellModel.itemSize = CGSizeMake(Zoom(350), Zoom(120));
            cellModel.cellSelectBlock = ^(NSIndexPath *indexPath) {
                RBGroupDetailVC *vc = RBGroupDetailVC.new;
                RBGroupListModel *model = [RanTool modelWithSection:indexPath.section row:indexPath.row dataSource:weak_self.dataSource];
                vc.id = model.group_id;
                [weak_self.navigationController pushViewController:vc animated:YES];
            };
            cellModel;
        })];
    }
    [self.collectionView reloadData];
    if(!arrayM.count)
    {
        [self.listView showPlaceHolder:RB_EMPTY_TIP tapBlock:nil];
    }
}


#pragma mark - Public

- (void)getData
{
    [self.listView.mj_header beginRefreshing];
}

#pragma mark - Network

- (NSString *)listPageKey
{
    return @"page";
}
- (NSDictionary *)listParams
{
    NSMutableDictionary *dicM = NSMutableDictionary.dictionary;
    dicM[@"cate_id"] = self.id;
    dicM[@"search_key"] = self.searchText;
    return dicM;
}
- (NSString *)listRequestPath
{
    return @"hu_group_list";
}
- (NSInteger)ListCurrentPage
{
    return self.response.currentPage;
}
- (BOOL)listLog
{
    return YES;
}
- (BOOL)listPost
{
    return YES;
}
- (Class)listResponseClass
{
    return RBGroupListResponse.class;
}
- (UIScrollView *)listView
{
    return self.collectionView;
}

- (void)getFirstPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadFirstPageWithDesView:self.listView showPlaceHolder:YES sucBlock:^(id data) {
        weak_self.response = data;
        [weak_self fillData];
    } failBlock:^(id data) {
        
    } reloadBlock:^{
        [weak_self getFirstPageData];
    }];
}

- (void)getNextPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadNextPageWithSucBlock:^(id data) {
        weak_self.response = data;
        [weak_self appendData];
    } failBlock:^(id data) {
        
    }];
}

#pragma mark - Event

- (void)filterBtnTapped
{
    RBGroupFilterPopVC *vc = RBGroupFilterPopVC.new;
    CGRect frame = [[RBAppDelegateHelper keyWindow] convertRect:self.filterView.frame fromView:self.filterView.superview];
    vc.topInWindow = frame.origin.y + frame.size.height;
    [self presentVc:vc];
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

kRBCollectionViewDataSourceAndDelegate(self.dataSource);

#pragma mark - Setter

#pragma mark - Getter

- (UIView *)filterView
{
    if (!_filterView)
    {
        self.filterView = ({
            UIView *view = UIView.new;
            view.backgroundColor = UIColor.whiteColor;
            
            [view view_addView:({
                UIView *view = UIView.new;
                view.backgroundColor = [RBThemeConfig lineGrayColor];
                view;
            }) withHeight:1 width:CGFLOAT_MIN top:5 bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
            
            UIView *filterBtn = [self filterBtnWithTitle:@"筛选"];
            [filterBtn rbAddTapGesWithTarget:self action:@selector(filterBtnTapped)];
            [view addSubview:filterBtn];
            [filterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_greaterThanOrEqualTo(Zoom(20));
                make.top.bottom.mas_equalTo(0);
                make.right.mas_equalTo(-Zoom(20));
            }];
            
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.mas_equalTo(0);
                make.height.mas_equalTo(Zoom(44));
            }];
            
            view;
        });
    }
    return _filterView;
}

- (UIView *)filterBtnWithTitle: (NSString *)title
{
    UIView *view = UIView.new;
    [view h_view_addView:[RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:title] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
    [view h_view_addView:({
        UIView *view = UIView.new;
        [view view_addView:({
            UIImageView *view = UIImageView.new;
            view.image = [UIImage renderImageWithName:@"shopFilterUp"];
            view.tintColor = [RBThemeConfig textBlackColor];
            view.tag = 333;
            view;
        }) withHeight:Zoom(5) width:Zoom(7) top:0 bottom:0 left:0 right:0 centerX:0];
        [view view_addView:({
            UIImageView *view = UIImageView.new;
            view.image = [UIImage renderImageWithName:@"shopFilterDown"];
            view.tintColor = [RBThemeConfig textBlackColor];
            view.tag = 444;
            view;
        }) withHeight:Zoom(5) width:Zoom(7) top:Zoom(2) bottom:0 left:0 right:0 centerX:0];
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:Zoom(6) right:0 centerY:YES];
    return view;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        self.collectionView = ({
            UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
            UICollectionView *view = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            view.dataSource = self;
            view.delegate = self;
            view.backgroundColor = UIColor.clearColor;
            view.mj_header = [RBRefreshHeader headerWithRefreshingTarget:self refreshingAction: @selector(getFirstPageData)];
            view.mj_footer = [RBRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(getNextPageData)];
            kRBIgnoreScrollViewInsetsAdjust(view);
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                make.bottom.mas_equalTo(0);
                make.top.mas_equalTo(0).offset(1);
            }];
            view;
        });
    }
    return _collectionView;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        self.dataSource = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
    }
    return _dataSource;
}

@end
